package com.example.hanfu.tiku;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hanfu.R;

import java.util.ArrayList;
import java.util.List;

public class DaTiActivity extends AppCompatActivity {
    private static final String TAG = "DaTiActivity";
    Button next;
    Button last;
    TextView corAnswer;
    private boolean wrongMode=false;
    DBTikuManager manager = new DBTikuManager(DaTiActivity.this);
    List<Tiku> timu;
    int count;
    int corrent;
    Tiku item;


    //int number=1;
    //HashMap<Integer,String> map=new HashMap<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent =getIntent();
        int id=intent.getIntExtra("position",0);
        Log.i(TAG,"获取的ID为："+id);
        corrent=id*5;
        count=corrent+5;

        List<Tiku> retList = new ArrayList<>();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_da_ti);
        TextView topic = findViewById(R.id.topic);
        RadioGroup option = findViewById(R.id.option);
        //选项
        RadioButton[] options = new RadioButton[4];
        options[0] = findViewById(R.id.A);
        options[1] = findViewById(R.id.B);
        options[2] = findViewById(R.id.C);
        options[3] = findViewById(R.id.D);
        next = findViewById(R.id.next);
        last = findViewById(R.id.last);
        corAnswer=findViewById(R.id.corAnswer);
        if(id==5||id==6){
            options[2].setVisibility(View.INVISIBLE);
            options[3].setVisibility(View.INVISIBLE);
        }



        //写入数据库

        Log.i(TAG, "DaTiActivity:写入数据库");
        timu=manager.listAll();
        //从数据库中获取数据
        showQuestion(corrent, topic, options[0], options[1], options[2], options[3], manager);

        //答案选中
        option.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < 4; i++) {
                    if (options[i].isChecked() == true) {
                        timu.get(corrent).setSelectedAnswer(i);
                        break;
                    }
                }
            }
        });
        //上一题
        last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (corrent == id*5) {
                    Toast.makeText(DaTiActivity.this,
                            "已是第一题", Toast.LENGTH_SHORT).show();
                } else {
                    corrent = corrent - 1;
                    showQuestion(corrent, topic, options[0], options[1], options[2], options[3], manager);
                    if(item.getSelectedAnswer() == -1){
                        //还没有选
                        option.clearCheck();
                    }else if (item.getSelectedAnswer() != -1) {
                        //已经选了
                        options[item.getSelectedAnswer()].setChecked(true);
                    }
                }
            }
        });
        //下一提
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = (String) next.getText();
                //判断是否作答
                if (option.getCheckedRadioButtonId() == -1) {
                    //没有选中
                    // 简单消息提示框
                    new AlertDialog.Builder(DaTiActivity.this)
                            .setMessage("请作答")
                            .setPositiveButton("确定", null)
                            .show();
                } else {
                    //已经作答，跳至下一页
                    if (corrent < count - 1) {
                        //跳转题目至下一题
                        corrent = corrent + 1;
                        showQuestion(corrent, topic, options[0], options[1], options[2], options[3], manager);
                        //清空单选
                        option.clearCheck();
                        //若之前已经选择过，则记录选择,设置选中
                        if (item.getSelectedAnswer() != -1) {
                            options[item.getSelectedAnswer()].setChecked(true);
                        }
                    } else if (corrent == count - 1 && wrongMode == true) {
                        //已是最后一页，且为错题模式
                        new AlertDialog.Builder(DaTiActivity.this).setTitle("提示").setMessage("已经到达最后一题，是否退出？")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        finish();
                                    }
                                }).setNegativeButton("取消", null).show();
                    } else {
                        //没有题目了，开始检测正确性
                        final List<Integer> wrongList = checkAnswer(timu,id*5);
                        if (wrongList.size() == 0) {
                            new AlertDialog.Builder(DaTiActivity.this)
                                    .setTitle("提示")
                                    .setMessage("你好厉害，答对了所有题！")
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            finish();
                                        }
                                    }).setNegativeButton("取消", null).show();
                        }
                        //窗口提示
                        new AlertDialog.Builder(DaTiActivity.this)
                                .setTitle("恭喜，答题完成！")
                                .setMessage("答对了" + (5 - wrongList.size()) + "道题" + "\n"
                                        + "答错了" + wrongList.size() + "道题" + "\n" + "是否查看错题？")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                wrongMode = true;
                                List<Tiku> newList = new ArrayList<Tiku>();
                                for (int i = 0; i < wrongList.size(); i++) {
                                    newList.add(timu.get(wrongList.get(i)));
                                }
                                timu.clear();
                                for (int i = 0; i < newList.size(); i++) {
                                    timu.add(newList.get(i));
                                }
                                corrent = 0;
                                count = timu.size();
                                showQuestion(corrent, topic, options[0], options[1], options[2], options[3], manager);
                                corAnswer.setVisibility(View.VISIBLE);
                            }}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        }).show();
                    }

                }
            }
        });

    }










    private void showQuestion(int number, TextView topic, RadioButton option_a, RadioButton option_b, RadioButton option_c, RadioButton option_d, DBTikuManager manager) {
        item = timu.get(number);
        //retList.add(new Tiku(item.getTopic(),item.getOption_a(),item.getOption_b(),item.getOption_c(),item.getOption_d(),item.getAnswer()));
        topic.setText(item.getTopic());
        Log.i(TAG, "da:topic:" + item.getTopic());
        option_a.setText(item.getOption_a());
        Log.i(TAG, "da:option_a:" + item.getOption_a());
        option_b.setText(item.getOption_b());
        Log.i(TAG, "da:option_b:" + item.getOption_b());
        option_c.setText(item.getOption_c());
        Log.i(TAG, "da:option_c:" + item.getOption_c());
        option_d.setText(item.getOption_d());
        Log.i(TAG, "da:option_d:" + item.getOption_d());
        corAnswer.setText(item.getCorAnswer());
    }

    //判断对错
    private List<Integer> checkAnswer(List<Tiku> list,int id) {
        List<Integer> wrongList = new ArrayList<>();
        for (int i = id; i <id+5; i++) {
            if (list.get(i).getAnswer() != list.get(i).getSelectedAnswer()){
                wrongList.add(i);
            }
        }
        return wrongList;
    }

}
